"""
Case Type   : bugfix
Case Name   : 设置behavior_compat_options ='proc_outparam_override'，自定义函数入参包含variadic类型，调用函数验证
Create At   : 2025/6/12
Owner       : liu-tong-8848
Description :
    1、设置behavior_compat_options ='proc_outparam_override'
    2、创建自定义函数入参包含variadic类型，in缺省
    3、调用函数
    4、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class BugFix(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()
        sql_cmd = "show behavior_compat_options;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertNotIn("ERROR", sql_res, '执行失败')
        self.behavior_compat_options = sql_res.strip().splitlines()[-2]

    def test_dolphin(self):
        self.log.info(f'----{os.path.basename(__file__)} start----')
        text = "----step1:设置behavior_compat_options ='proc_outparam_override'； expect:成功----"
        self.log.info(text)
        sql_cmd = "set behavior_compat_options = 'proc_outparam_override';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)


        text = '----step2:创建自定义函数入参包含variadic类型，in缺省；expect:成功----'
        self.log.info(text)
        sql_cmd = "drop function if exists f_fixed_110; " \
                  + "create function f_fixed_110(c4 variadic integer[]) return int " \
                  + "is begin raise notice 'c4 = %', c4; return 1; end;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.DROP_FUNCTION_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertIn(self.constant.CREATE_FUNCTION_SUCCESS_MSG, sql_res, '执行失败' + text)

        text = '----step3:调用函数；expect:成功----'
        self.log.info(text)
        sql_cmd = "select f_fixed_110(1, 2, 3);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn("f_fixed_110", sql_res, '执行失败' + text)
        self.assertIn("(1 row)", sql_res, '执行失败' + text)
        self.assertNotIn("ERROR", sql_res, '执行失败' + text)

    def tearDown(self):
        text = '----step4:清理环境；expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop function if exists f_fixed_110; set behavior_compat_options = '{self.behavior_compat_options}';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.DROP_FUNCTION_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.log.info(f'----{os.path.basename(__file__)} end-----')
